/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.login.controller;

import com.login.user.ActiveUser;
import javax.faces.bean.SessionScoped;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.SessionScope;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Ondrej
 */

@Controller
public class LoginController {
    
    @Autowired
    private ActiveUser activeUser;
    
    public static Logger log = Logger.getLogger(LoginController.class.getName());
    
        
        @RequestMapping(value="/login", method = RequestMethod.GET)
	public ModelAndView login() {
            ModelAndView mav = new ModelAndView("login");
            log.debug("attempting login controller");
            return mav;
 
	}
        
	@RequestMapping(value="/home", method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
                
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//		String name = user.getUsername();
                activeUser.setUsername(user.getUsername());
                activeUser.setRole(user.getAuthorities().toArray()[0].toString());
                activeUser.setPerson_id(2);
		model.addAttribute("user", activeUser);
//		model.addAttribute("message", "Spring Security login + database example");
		return "home";
 
	}        
        
        @RequestMapping(value="/login-failed", method = RequestMethod.GET)
	public ModelAndView loginerror() {
            ModelAndView mav = new ModelAndView("login");
            mav.addObject("error", "true");
            return mav;
 
	}
        
	@RequestMapping(value="/logout", method = RequestMethod.GET)
	public String logout(ModelMap model) {
            return "login";
 
	}        
    
}
